const lbbService = require("../../../service/service")

// pages/marketTool/putinthos/hosMarketer/hosMarketer.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        putinDate: '',
        marketerUserList: [],
        marketerUserObjList: [],
        hosLevelList: [],
        optType: 'add',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        //进入的类型     
        this.setData({
            optType: options.type,
            hmId: options.id
        })
        console.log("onLoad--opitons=", options)
        this.getHosMarketBaseInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let that = this
        if (!!that.data.changedLocationInfo) {
            console.log("hospitalInfo:", that.data.changedLocationInfo)
        }
        console.log("chooseAgentInfo=", that.data.chooseAgentInfo)
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    bindPutinDateChange(e) {
        this.setData({
            putinDate: e.detail.value
        })
    },
    bindContractStartDateChange(e) {
        this.setData({
            contractStartDate: e.detail.value
        })
    },
    bindContractEndDateChange(e) {
        this.setData({
            contractEndDate: e.detail.value
        })
    },
    bindPickerMarketUserChange(e) {
        let that = this
        let index = e.detail.value
        this.setData({
            marketerUserIndex: index,
            putinUserPhone: that.data.marketerUserObjList[index].phone,
            putinUserName: that.data.marketerUserObjList[index].username
        })
    },
    bindPickerHosLevelChange(e) {
        let that = this
        let index = e.detail.value
        this.setData({
            hosLevelIndex: index,
            hosLevel: that.data.hosLevelList[index],
        })
    },
    formSubmit(e) {
        let data = e.detail.value
        console.log('form发生了submit事件，携带数据为：', e.detail.value)
        if (!data.putinDate) {
            delete data.putinDate
        }
        if (!data.contractEndDate || !data.contractStartDate) {
            delete data.contractEndDate
            delete data.contractStartDate
        }

        if (this.data.optType == 'update') {

            lbbService.putinUpdateHosMarketerInfo(data, function (res) {
                if (res.result == 0) {
                    wx.showToast({
                        title: '更新成功',
                    })
                    wx.redirectTo({
                        url: '../putinhos',
                    })
                } else {
                    wx.showToast({
                        title: res.description,
                    })
                }
            })
            return
        }

        lbbService.putinAddHosMarketerInfo(data, function (res) {
            if (res.result == 0) {
                wx.showToast({
                    title: '添加成功',
                })
                wx.redirectTo({
                    url: '../putinhos',
                })
            } else {
                wx.showToast({
                    title: res.description,
                })
            }
        })
    },

    formReset(e) {
        console.log('form发生了reset事件，携带数据为：', e.detail.value)
        this.setData({
            chosen: ''
        })
    },

    chooseHospital: function () {
        wx.navigateTo({
            url: '/pages/chooseHospital/chooseHospital',
        })
    },

    chooseAgentInfo: function () {
        wx.navigateTo({
            url: '/pages/marketTool/putinthos/chooseAgentInfo/chooseAgentInfo',
        })
    },

    getHosMarketBaseInfo() {
        let that = this
        lbbService.getPutinBaseDataInfo({}, function (res) {
            console.log("getHosMarketBaseInfo=", res)
            that.data.marketerUserList = []
            res.marketerInfoList.forEach(element => {
                that.data.marketerUserList.push(element.username)
            });
            that.setData({
                hosLevelList: res.hosLevelList,
                marketerUserObjList: res.marketerInfoList,
                marketerUserList: that.data.marketerUserList
            })
            if(that.data.marketerUserObjList.length == 1) {
                that.setData({
                    marketerUserIndex: 0,
                    putinUserPhone: that.data.marketerUserObjList[0].phone,
                    putinUserName: that.data.marketerUserObjList[0].username
                })
            }
            if (that.data.optType == 'update') {
                that.getHosMarketById()
            }
        })
    },


    getHosMarketById() {
        let that = this
        lbbService.getPutinHosMarketerList({
            id: that.data.hmId
        }, function (res) {
            console.log("getHosMarkerById--->", res)
            if (res.result == 0 && res.data.list.length > 0) {
                let hmData = res.data.list[0]
                that.data.hosLevelList.forEach((element, index) => {
                    if (element == hmData.hosLevel) {
                        that.setData({
                            hosLevelIndex: index
                        })
                    }
                });
                that.data.marketerUserList.forEach((el, index) => {
                    if (el == hmData.putinUserName) {
                        that.setData({
                            marketerUserIndex: index
                        })
                    }
                });
                that.setData({
                    hmData: hmData,
                    changedLocationInfo: {
                        hospitalName: hmData.hosName,
                        hospitalId: hmData.lbmId
                    },
                    chooseAgendInfo: {
                        id: hmData.agentId,
                        concat: hmData.agentConcat
                    },
                    hosLevel: hmData.hosLevel,
                    putinUserName: hmData.putinUserName,
                    putinUserPhone: hmData.putinUserPhone,
                    putinDate: hmData.putinDate,
                    contractStartDate: hmData.contractStartDate,
                    contractEndDate: hmData.contractEndDate,
                    hosCommissionRatio: hmData.hosCommissionRatio,
                    agentCommissionRatio: hmData.agentCommissionRatio,
                    memo: hmData.memo
                })

                let agentId = hmData.agentId
                if (agentId > 0) {
                    lbbService.getPutinAgentList({
                        id: agentId
                    }, function (aRes) {
                        if (aRes.result == 0 && aRes.data.list.length > 0) {
                            let agentData = aRes.data.list[0]
                            that.setData({
                                chooseAgendInfo: {
                                    id: agentData.id,
                                    concat: agentData.concat
                                }
                            })
                        }
                    })
                }

            } else {
                wx.showToast({
                    title: '获取失败',
                })
            }
        })
    }

})